using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using MoControls.Controls;
using MoControls.Skins;

namespace MoControls
{
    public class XNAUIManager
        : DrawableGameComponent, IUIManager, IDisposable
    {

        #region Events

        #endregion

        #region Fields
                
        private List<Screen> screens = null;
        private RenderTarget2D renderTarget = null;
        private XNARendererBase renderer = null;
        private AvailableRenderers selectedRenderer = AvailableRenderers.Default;
        private SkinManager skinManager = null;

        private bool initialized = false;
        private int screenWidth = 0;
        private int screenHeight = 0;        
        private float defaultLayerDepth = 0.0F;
        
        #endregion

        #region Properties

        public bool Initialized
        {
            get
            {
                return this.initialized;
            }
            private set
            {
                this.initialized = value;
            }
        }

        public int ScreenWidth
        {
            get
            {
                return this.screenWidth;
            }
            set
            {
                this.screenWidth = value;
            }
        }

        public int ScreenHeight
        {
            get
            {
                return this.screenHeight;
            }
            set
            {
                this.screenHeight = value;
            }
        }

        public float DefaultLayerDepth
        {
            get
            {
                return this.defaultLayerDepth;
            }
            private set
            {
                this.defaultLayerDepth = value;
            }
        }

        public SkinManager SkinManager
        {
            get
            {
                return this.skinManager;
            }
        }

        #endregion

        #region .ctor

        public XNAUIManager(Game game)
            : base(game)
        {  

            this.Initialize();
        }

        #endregion

        #region Methods

        #region Initialization Methods

        public override void  Initialize()
        {  
            base.Initialize();            

            // Initializes controls list
            this.screens = new List<Screen>();
            this.GraphicsDevice.DeviceReset += new EventHandler<EventArgs>(GraphicsDevice_DeviceReset);

            // For new, only default renderer is supported.
            switch (this.selectedRenderer)
            {
                default:
                case AvailableRenderers.Default:
                    this.renderer = new XNADefaultRenderer(this);
                    break;
            }

            this.renderTarget = this.CreateRenderTarget();
            this.skinManager = new SkinManager();
            this.Initialized = true;            
        }       

        /// <summary>
        /// Create a RenderTarget2D object of the screen dimension for rendering layers over it.
        /// </summary>
        /// <returns></returns>
        private RenderTarget2D CreateRenderTarget()
        {
            RenderTarget2D target = null;

            target = new RenderTarget2D(this.GraphicsDevice, this.ScreenWidth, this.ScreenHeight);
            return target;
        }

        private RenderTarget2D CreateRenderTarget(int screenHeight, int  screenWidth)
        {
            RenderTarget2D target = null;

            target = new RenderTarget2D(this.GraphicsDevice, screenHeight, screenWidth);
            return target;
        }

        #endregion

        #region Screen methods

        public Screen CreateScreen()
        {
            throw new NotImplementedException();
        }
        
        #endregion

        #region Disposable

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);            
        }
        
        #endregion

        #endregion

        #region Event Handlers

        private void GraphicsDevice_DeviceReset(object sender, EventArgs e)
        {
            
        }

        #endregion
    }
}
